<template>
  <view class="cart">
   
   <!-- 地址 -->
   
   <!-- 地址 -->
    <!-- 1 列表 -->
    <view
      class="cart-list"
      v-for="(item, index) in cart_goods"
      :key="item.goods_id"
    >
      <view class="cart-item">
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox
            :value="item.checked"
            shape="circle"
            active-color="red"
            @change="checkClickFn(item.goods_id)"
          ></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            width="191rpx"
            height="191rpx"
            :src="item.goods_small_logo"
          ></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{ item.goods_name }}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{ item.goods_price }}</view>
            <view class="goods-num-tool">
              <view class="num-btn" @click="changeNum(-1, index)">-</view>
              <view class="goods-vnum">{{ item.count }}</view>
              <view class="num-btn" @click="changeNum(1, index)">+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <u-checkbox
          :value="checkAll"
          shape="circle"
          active-color="red"
          @change="checkAllFn"
          >全选</u-checkbox
        >
      </view>
      <view class="all-price">
        合计: <text>￥{{ check_mony }}</text>
      </view>
      <view class="all-count">
        <!-- <navigator url="/pages/order/order"> -->
        <u-button type="error" shape="circle" @click="to_order">
          去结算({{ check_count }})
        </u-button>
        <!-- </navigator> -->
      </view>
    </view>
  </view>
</template>

<script>
// import { mapState } from 'vuex'
export default {
  onLoad(){
    console.log(this.cart_goods);
  },
  methods: {
    //点击去结算按钮
    to_order() {
      uni.navigateTo({
        url: "/pages/pay/pay",
      });
    },
    //点击全选按钮触发
    checkAllFn() {
      //取反
      const checkAll = !this.checkAll;
      this.$store.commit("cart/goods_checked", checkAll);
    },
    //点击加减按钮 触发
    async changeNum(params, index) {
      //调用vuex对数量更改前先做判断,如果数量为一而且点击减一 ,就询问是否要删除

      if (params === -1 && this.cart_goods[index].count === 1) {
        const [err, res] = await uni.showModal({
          title: "提示",
          content: "是否要删除该商品",
        });
        if (res.confirm) {
          this.$store.commit("cart/goods_remove", index);
        }
      } else {
        //调用vuex的方法
        this.$store.commit("cart/goods_count_change", { params, index });
      }
    },
    //点击复选框
    checkClickFn(id) {
      //将复选框中的状态改为选中
      //调用vuex中的选中方法
      this.$store.commit("cart/checkCart", id);
    },
  },
  data() {
    return {
      // cart_goods: this.$store.state.cart.cart,
    };
  },
  computed: {
    cart_goods(){
      return this.$store.state.cart.cart
    },
    cart_count() {
      return this.cart_goods.reduce((num, item) => num + item.count, 0);
    },
    //购物车单选与全选功能
    checkAll() {
      {
        return this.cart_goods.every((item) => item.checked == true);
      }
    },
    //选中数量
    check_count() {
      return this.cart_goods.reduce((sum, item) => {
        if (item.checked) {
          return sum + item.count;
        } else {
          return sum;
        }
      }, 0);
    },
    //合计价格
    check_mony() {
      return this.cart_goods.reduce((sum, item) => {
        if (item.checked) {
          return sum + item.count * item.goods_price;
        } else {
          return sum;
        }
      }, 0);
    },
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: red;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }

            .goods-vnum {
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
}
</style>